package com.telpoo.bhlib.share;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.telpoo.bhlib.Bhdefi;
import com.telpoo.bhlib.share.twitter.ShareTwitterActivity;
import com.telpoo.frame.utils.DeviceSupport;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

public class BHShareUtils {

	public static String uploadImgur(String path, String clientId) {
		try {
			File file = new File(path);
			final String upload_to = "https://api.imgur.com/3/upload.json";
			String API_key = "27905d84c9ec40a";
			if (clientId != null)
				API_key = clientId;
			HttpClient httpClient = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost httpPost = new HttpPost(upload_to);
			httpPost.setHeader("Authorization", "Client-ID " + API_key);

			final MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
			entity.addPart("image", new FileBody(file));
			entity.addPart("key", new StringBody(API_key));
			entity.addPart("description", new StringBody("test upimgae"));
			httpPost.setEntity(entity);
			final HttpResponse response = httpClient.execute(httpPost, localContext);
			final String response_string = EntityUtils.toString(response.getEntity());
			// return response_string;
			final JSONObject json = new JSONObject(response_string);
			String link = json.getJSONObject("data").getString("link");
			// link = "http://i.imgur.com/"+link+"l.png";
			return link;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static void rating(Activity activity) {
		String id = activity.getApplicationContext().getPackageName();
		try {
			Uri marketUri = Uri.parse("market://details?id=" + id);
			Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
			activity.startActivity(marketIntent);
		} catch (Exception e) {
			Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + id));
			activity.startActivity(marketIntent);
		}
	}

	public static void emailSupport(Activity activity, String pathImage, String subject, String text) {

		// Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		// emailIntent.setType("image/png");
		//
		// emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
		//
		// if (pathImage != null) {
		// Uri uri1 = Uri.parse("file://" + pathImage);
		// emailIntent.putExtra(Intent.EXTRA_STREAM, uri1);
		// }
		// Intent a = Intent.createChooser(emailIntent, "Sharing options");
		// activity.startActivity(a);

		sentEmail(activity, Bhdefi.emailSupport, pathImage, subject, text);

	}

	public static void sentEmail(Activity activity, String emailAdress, String pathImage, String subject, String text) {

		Intent email1 = new Intent(Intent.ACTION_SEND);
		email1.putExtra(Intent.EXTRA_EMAIL, new String[] { "" + emailAdress });
		email1.putExtra(Intent.EXTRA_SUBJECT, subject);
		email1.putExtra(Intent.EXTRA_TEXT, text);

		if (pathImage != null) {
			Uri uri1 = Uri.parse("file://" + pathImage);
			email1.putExtra(Intent.EXTRA_STREAM, uri1);
		}

		email1.setType("message/rfc822");
		activity.startActivity(Intent.createChooser(email1, "Select mail client"));

	}

	public static void shareTwitter(Activity ac, String status, String urlImg) {

		Intent intent = new Intent(ac, ShareTwitterActivity.class);
		if (urlImg != null)
			intent.putExtra("url-picture", urlImg);
		intent.putExtra("status", status);

		ac.startActivity(intent);

	}

}
